Slave- 

#include <main.h>
unsigned int read_keys(unsigned int mask);

void main() {
   unsigned char key = 0, flag = 0;

   while(TRUE) {
      while(flag == 0) {
         key = read_keys(0x0F); // buttons A0–A3
         if(key) flag = 1;
      }
      flag = 0;
      printf("%c", key);
      while(read_keys(0x0F));  // debounce
   }
}

unsigned int read_keys(unsigned int mask) {
   unsigned int val = input_a();
   return val & mask;
}


Master- 

#include <main.h>

// define all the LCD pins like usual
#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN     PIN_D0
#define LCD_RW_PIN     PIN_D1
#define LCD_DATA4      PIN_D4
#define LCD_DATA5      PIN_D5
#define LCD_DATA6      PIN_D6
#define LCD_DATA7      PIN_D7

#include <lcd.c>

unsigned int input_char;  // holds command received

// declare all the light functions up top
void headlights(void);
void right_indicator(void);
void left_indicator(void);
void hazards(void);

void main() {
   lcd_init();  // fire up the LCD

   // make sure all lights are off to start
   output_low(PIN_B0); // headlights
   output_low(PIN_B1); // right indicator
   output_low(PIN_B2); // left indicator
   output_low(PIN_A3); // hazards (just using this for trigger)

   while(TRUE) {
      if(kbhit()) {            // if we got something from master
         input_char = getch(); // read that input

         // decide what to do based on the code
         switch(input_char) {
            case 0x01:
               headlights();      // if headlight button
               break;
            case 0x02:
               right_indicator(); // if right indicator
               break;
            case 0x04:
               left_indicator();  // if left indicator
               break;
            case 0x08:
               hazards();         // hazards button held down
               break;
            default:
               break;             // if it’s junk input just ignore
         }
      }
   }
}

// turn on headlights for 1 sec then off again
void headlights(void) {
   lcd_putc("\fHEADLIGHTS ON");
   output_high(PIN_B0);
   delay_ms(1000);
   output_low(PIN_B0);
}

// flashes right indicator 6 times (about 3 sec total)
void right_indicator(void) {
   lcd_putc("\fRIGHT INDICATOR");
   for(int i = 0; i < 6; i++) {
      output_high(PIN_B1);
      delay_ms(250);
      output_low(PIN_B1);
      delay_ms(250);
   }
   output_low(PIN_B1); // just make sure it's off after
}

// same as right one but for the left indicator
void left_indicator(void) {
   lcd_putc("\fLEFT INDICATOR");
   for(int i = 0; i < 6; i++) {
      output_high(PIN_B2);
      delay_ms(250);
      output_low(PIN_B2);
      delay_ms(250);
   }
   output_low(PIN_B2);
}

// hazard mode - flashes both sides while button is held
void hazards(void) {
   lcd_putc("\fHAZARDS ACTIVE");
   while(TRUE) {
      if(kbhit()) { // check for a new command
         input_char = getch();
         if(input_char != 0x08) { // if it’s NOT hazards again, stop
            output_low(PIN_B1);
            output_low(PIN_B2);
            break;
         }
      }
      // blink both indicators at the same time
      output_high(PIN_B1);
      output_high(PIN_B2);
      delay_ms(300);
      output_low(PIN_B1);
      output_low(PIN_B2);
      delay_ms(300);
   }
}

